<?php
declare (strict_types = 1);

namespace app\wechat\controller;

use app\BaseController;
use app\Request;
use app\wechat\model\AuthTokenModel;
use app\wechat\model\OfficeUserModel;
use app\wechat\model\WxpayOrderModel;
use app\wechat\service\MiniService;
use app\wechat\service\OfficeService;
use app\wechat\service\OfficeUserService;
use app\wechat\service\WxpayService;
use Exception;
use think\facade\View;

class Index extends BaseController
{
    /**
     * 授权页面
     *
     * @Author tanruizheng
     * @DateTime 2021-08-10
     * @return void
     */
    public function index(){
        //授权地址
        $url = url('Index/ouath',['appid'=> 'wx22650a4626757a41'],true,true)->__toString();
        return View::fetch('index',[
            'url' => $url
        ]);
    }

    /**
     * 网页授权
     *
     * @Author tanruizheng
     * @DateTime 2021-08-10
     * @return void
     */
    public function ouath(Request $request){
        $appid = $request->param('appid');
        $callback = url('Index/callback',['appid'=>$appid],true,true)->__toString();
        $office = new OfficeService($appid);
        $response = $office->getApp()->oauth->scopes(['snsapi_userinfo'])
                        ->redirect($callback);
        $response->send();
    }

    /**
     * 网页授权回调地址
     *
     * @Author tanruizheng
     * @DateTime 2021-08-10
     * @param Request $request
     * @return void
     */
    public function callback(Request $request){
        $appid = $request->param('appid');
        $res = OfficeUserService::ouathUser($appid);
        if($res['status']){
            $code = $res['data']['code'];
            $redirectUrl = url('Index/wechatLoginSuccess',['code'=>$code],true,true)->__toString();
            return redirect($redirectUrl);
        }else{
            throw new Exception('信息授权失败');
        }
    }

    public function wechatLoginSuccess(Request $request){
        $code = $request->param('code');
        $openid = AuthTokenModel::where(['code' => $code])->value('open_id');
        $officeUser = OfficeUserModel::where('open_id',$openid)->findOrEmpty()->toArray();
        echo '登陆成功' . PHP_EOL;
        echo '登陆信息' . PHP_EOL;
        echo "<pre>";
        print_r($officeUser);
        echo "</pre>";
    }

     /**
     * 微信小程序用户信息授权
     *
     * @Author tanruizheng
     * @DateTime 2021-08-17
     * @return void
     */
    public function miniAuthUserInfo(Request $request){
        $miniService = new MiniService('wx294975311d4ef718');
        $code = $request->param('code');
        $iv = $request->param('iv');
        $encryptedData = $request->param('encryptedData');
        $userInfo = $miniService->getUserInfo($code,$iv,$encryptedData);
        return json($userInfo);
    }

     /**
     * 微信小程序手机号码授权
     *
     * @Author tanruizheng
     * @DateTime 2021-08-17
     * @return void
     */
    public function getPhoneNumber(Request $request){
        $miniService = new MiniService('wx294975311d4ef718');
        $code = $request->param('code');
        $iv = $request->param('iv');
        $encryptedData = $request->param('encryptedData');
        $userInfo = $miniService->getPhoneNumber($code,$iv,$encryptedData);
        return json($userInfo);
    }

    /**
     * 微信支付回调地址
     *
     * @Author tanruizheng
     * @DateTime 2021-08-18
     * @return void
     */
    public function notifyUrl(){
        try {
            $wxpayService = new WxpayService('wx294975311d4ef718');
            $response = $wxpayService->handlePayNotify(function($message, $fail){
                if($message['return_code'] === 'SUCCESS'){
                    if( $message['result_code'] === 'SUCCESS' ){
                        //把订单状态改成已支付
    
                    }else{
                        //把订单状态改成已支付
    
                    }
                    return true;
                }else{
                    return $fail('通信失败,请稍候通知我');
                }
            });
            $response->send();
        } catch (\Exception $e) {
            return $e->getMessage();
        }
    }
}
